package BackTracking;

import java.util.ArrayList;
import java.util.List;

/**
 * Lettcode 77: https://leetcode-cn.com/problems/combinations/
 */
public class combine {
    private int n;

    private int k;

    public static void main(String[] args) {
        int n = 4;
        int k = 2;
        System.out.println(new combine().combine(n, k));
    }

    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        if (n < 1 || k <= 0) {
            return res;
        }
        this.n = n;
        this.k = k;
        List<Integer> curRes = new ArrayList<>();
        int curNum = 0;
        backTracking(res, curRes, 1);
        return res;
    }

    private void backTracking(List<List<Integer>> res, List<Integer> curRes, int curNum) {
        if (curRes.size() == k) {
            res.add(new ArrayList<>(curRes));
            return;
        }
        for (int i = curNum; i <= n; i++) {
            curRes.add(i);
            backTracking(res, curRes, i + 1);
            curRes.remove(curRes.size() - 1);
        }
    }
}
